import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';

class AppBarPage extends StatefulWidget {
  const AppBarPage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _AppBarPageState createState() => _AppBarPageState();
}

class _AppBarPageState extends State<AppBarPage> {
  List _images = [
    "https://d.ifengimg.com/w480_h270_q90_webp/x0.ifengimg.com/ucms/2023_42/B82DEDEB2E05B97B54527FAF202E28C3509A4CFA_size276_w480_h270.png",
    "https://d.ifengimg.com/w480_h270_q90_webp/x0.ifengimg.com/res/2023/719E34ADB788305F2A81E8F24F40BAD93405738A_size83_w640_h394.jpg",
    "https://d.ifengimg.com/w480_h270_q90_webp/x0.ifengimg.com/ucms/2023_36/5B8B57B1863E4D988D1406E7B6DD2B792C2B92A8_size76_w1080_h540.jpg",
    "https://d.ifengimg.com/w480_h270_q90_webp/x0.ifengimg.com/ucms/2023_42/BCE50FE08CEAF6863A83175DA4C405B9375ACCC7_size76_w975_h549.jpg",
  ];
  double _appBarAlpha = 0;

  @override
  void initState() {
    super.initState();
  }

  _onScroll(offset) {
    print("得到滚动值--->$offset");
    double alpha = offset / 100;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      _appBarAlpha = alpha;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          MediaQuery.removePadding(
            context: context,
            //移除顶部top
            removeTop: true,
            child: NotificationListener(
              onNotification: (scrollNotification) {
                if (scrollNotification is ScrollUpdateNotification &&
                    scrollNotification.depth == 0) {
                  //是滚动，且是列表滚动

                  _onScroll(scrollNotification.metrics.pixels);
                  return true;
                } else {
                  return false;
                }
              },
              child: ListView(
                children: [
                  Container(
                    height: 169,
                    child: Swiper(
                      itemCount: _images.length,
                      autoplay: true,
                      onTap: (index) {
                        print("object$index");
                      },
                      itemBuilder: (BuildContext context, int index) {
                        return Image.network(
                          _images[index],
                          fit: BoxFit.cover,
                        );
                      },
                      //指示器
                      pagination: SwiperPagination(),
                    ),
                  ),
                  Container(
                    height: 800,
                    child: ListTile(
                      title: Text("我是标题"),
                    ),
                  )
                ],
              ),
            ),
          ),
          Opacity(
            //控制透明度渐变控件
            opacity: _appBarAlpha,
            child: Container(
              height: 80,
              decoration: BoxDecoration(color: Colors.white),
              child: Center(
                child: Padding(
                  padding: EdgeInsets.only(top: 20),
                  child: Text("首页"),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
